modifiers = ["left_shift", "left_command", "left_option", "left_control"]

# permute all possible combinations of modifiers using permutations from itertool
import itertools
meta_combo = itertools.product([True, False], repeat=len(modifiers))
meta_combo = list(meta_combo)
meta_combo.sort(key=lambda x: sum(x))
print(meta_combo)

essential_modifiers = ["right_command", "right_control", "right_shift", "right_option"]


group = "F1-F12"
if group == "Navigation":
    description = "Capslock++ Navigation"
    map_list = [
        ("h", "left_arrow"),
        ("j", "down_arrow"),
        ("k", "up_arrow"),
        ("l", "right_arrow"),
        ("u", "page_down"),
        ("i", "page_up")
    ]
elif group == "F1-F12":
    description = "Capslock++ F1-F12"
    map_list = [
        ("1", "f1"),
        ("2", "f2"),
        ("3", "f3"),
        ("4", "f4"),
        ("5", "f5"),
        ("6", "f6"),
        ("7", "f7"),
        ("8", "f8"),
        ("9", "f9"),
        ("0", "f10"),
        ("hyphen", "f11"),
        ("equal_sign", "f12")
    ]
    
    
f = open("output.json", 'w')
f.write(f"""
{{
    "description": "{description}",
    "manipulators": [
""")

rule_list = []
for original, target in map_list:
    for combo in meta_combo:
        modifier_list = [modifiers[i] for i in range(len(modifiers)) if combo[i]]
        # original = "h"
        # target = "left_arrow"
        u = modifier_list+[original]
        v = modifier_list+[target]
        mandatory = modifier_list + essential_modifiers
        mandatory_with_quote = [f'"{m}"' for m in mandatory]
        modifier_list_with_quote = [f'"{m}"' for m in modifier_list]
        s = f"""
    {{
        "description": "{' + '.join(u)} = {' + '.join(v)}",
        "type": "basic",
        "from": {{
            "key_code": "{original}",
            "modifiers": {{
                "mandatory":  [{','.join(mandatory_with_quote)}]
            }}
        }},
        "to": [{{
            "key_code": "{target}",
            "modifiers": [{','.join(modifier_list_with_quote)}]
        }}]
    }}
    """
        rule_list.append(s)


f.write(','.join(rule_list))
f.write(f"]\n}}\n")
    
f.close()